home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / mosml / src / mosmllib / Vector.mlp < prev    next >
Encoding:
Text File  |  1996-07-03  |  3.1 KB  |  114 lines  |  [TEXT/R*ch]

  1. (* Vector.sml -- new basis *)
  2.  
  3. type 'a vector = 'a vector;
  4.  
  5. #include "../config/m.h"
  6. #ifdef SIXTYFOUR
  7. val maxLen = 18014398509481983; (* = 2^54-1, for 64-bit architectures *)
  8. #else
  9. val maxLen = 4194303;       (* = 2^22-1, for 32-bit architectures *)
  10. #endif
  11.  
  12. local
  13.     prim_val vector_ : int -> 'x -> 'a vector         = 2 "make_vect";
  14.     prim_val sub_    : 'a vector -> int -> 'a         = 2 "get_vect_item";
  15.     prim_val update_ : 'a vector -> int -> 'a -> unit = 3 "set_vect_item";
  16. in
  17.  
  18. prim_val length : 'a vector -> int                   = 1 "vect_length";
  19.  
  20. fun fromList (vs : 'a list) =
  21.   let val n = List.length vs
  22.       val a = if n > maxLen then raise Size else vector_ n () : 'a vector
  23.       fun init [] i = ()
  24.         | init (v::vs) i = (update_ a i v; init vs (i+1))
  25.   in (init vs 0; a) end;
  26.  
  27. fun tabulate(n, f : int -> 'a) =
  28.   if n < 0 orelse n > maxLen then raise Size else
  29.   let val a = vector_ n () : 'a vector
  30.       fun init i = if i >= n then () else (update_ a i (f i); init (i+1))
  31.   in (init 0; a) end;
  32.  
  33. fun sub(v, i) =
  34.     if i < 0 orelse i >= length v then raise Subscript
  35.     else sub_ v i;
  36.  
  37. fun extract (vec : 'a vector, i, sliceend) =
  38.     let val n = case sliceend of NONE => length vec - i | SOME n => n
  39.     val newvec = if i<0 orelse n<0 orelse i+n > length vec then
  40.                      raise Subscript
  41.              else
  42.              vector_ n () : 'a vector
  43.     fun copy j = if j<n then (update_ newvec j (sub_ vec (i+j)); 
  44.                   copy (j+1))
  45.              else ()
  46.     in copy 0; newvec end;
  47.  
  48. fun concat vecs =
  49.     let fun acc [] len       = len
  50.       | acc (v1::vr) len = acc vr (length v1 + len)
  51.     val len = acc vecs 0
  52.     val newvec = if len > maxLen then raise Size else vector_ len ()
  53.     fun copyall to []       = ()
  54.       | copyall to (v1::vr) =
  55.         let val len1 = length v1
  56.         fun copy j =
  57.             if j<len1 then
  58.             (update_ newvec (to+j) (sub_ v1 j); copy (j+1))
  59.             else
  60.             ()
  61.         in copy 0; copyall (to+len1) vr end
  62.     in copyall 0 vecs; newvec end;
  63.  
  64.  
  65. fun foldl f e a = 
  66.     let    val stop = length a
  67.     fun lr j res = if j < stop then lr (j+1) (f(sub_ a j, res))
  68.                else res
  69.     in lr 0 e end
  70.  
  71. fun foldr f e a =
  72.     let    fun rl j res = if j >= 0 then rl (j-1) (f(sub_ a j, res))
  73.                else res
  74.     in rl (length a - 1) e end
  75.  
  76. fun app f a = 
  77.     let val stop = length a
  78.     fun lr j = if j < stop then (f(sub_ a j); lr (j+1))
  79.            else ()
  80.     in lr 0 end
  81.  
  82. fun sliceend (a, i, NONE) = 
  83.         if i<0 orelse i>length a then raise Subscript
  84.     else length a
  85.   | sliceend (a, i, SOME n) = 
  86.     if i<0 orelse n<0 orelse i+n>length a then raise Subscript
  87.     else i+n;
  88.  
  89. fun foldli f e (slice as (a, i, _)) = 
  90.     let fun loop stop =
  91.         let fun lr j res = 
  92.         if j < stop then lr (j+1) (f(j, sub_ a j, res))
  93.         else res
  94.         in lr i e end
  95.     in loop (sliceend slice) end;
  96.  
  97. fun foldri f e (slice as (a, i, _)) = 
  98.     let fun loop start =
  99.         let fun rl j res = 
  100.             if j >= i then rl (j-1) (f(j, sub_ a j, res))
  101.             else res
  102.         in rl start e end;
  103.     in loop (sliceend slice - 1) end
  104.  
  105. fun appi f (slice as (a, i, _)) = 
  106.     let fun loop stop = 
  107.         let    fun lr j = 
  108.             if j < stop then (f(j, sub_ a j); lr (j+1)) 
  109.             else ()
  110.         in lr i end
  111.     in loop (sliceend slice) end;
  112.  
  113. end
  114.